<?php

namespace App\Admin\Controllers;

use App\Models\Count;
use App\Models\User;
use Carbon\Carbon;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class CountController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '每日CINV提交记录';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Count);
        $grid->column('id', __('记录序号'));
        $grid->column('user_id', __('用户名'))->display(function ($user_id){
            return User::where('id',$user_id)->value('username');
        });
        $grid->column('nausea', __('恶心次数'));
        $grid->column('vomit', __('呕吐次数'));
        $grid->column('taking_time', __('开始日期'))->display(function (){
            $taking_time = User::where('id',$this->user_id)->value('taking_time');
            return date('Y/m/d',strtotime($taking_time));
        });
        $grid->column('updated_at', __('记录日期'))->display(function ($updated_at){
            //Carbon::now()->format('')
            $taking_time = User::where('id',$this->user_id)->value('taking_time');
            $datetime_start = new \DateTime(date('Y-m-d',strtotime($updated_at)));
            $datetime_end = new \DateTime(date('Y-m-d',strtotime($taking_time)));
            $days = $datetime_start->diff($datetime_end)->days+1;

            return '第'.$days.'天 ('.date('Y/m/d',strtotime($updated_at)).')';
        });
        $grid->disableCreateButton();
        $grid->disableColumnSelector();
        $grid->disableBatchActions();
        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->like('username', '用户名');
            $filter->date('updated_at', '上次更新日期');

        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Count::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('user_id', __('用户名'))->as(function ($user_id){
            return User::where('id',$user_id)->value('username');
        });
        $show->field('nausea', __('恶心次数'));
        $show->field('vomit', __('呕吐次数'));
        $show->field('created_at', __('提交时间'));
        $show->field('updated_at', __('更新时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Count);

        $form->number('user_id', __('User id'));
        $form->number('nausea', __('恶心次数'));
        $form->number('vomit', __('呕吐次数'));

        $form->tools(function (Form\Tools $tools) {

            // 去掉`删除`按钮
            $tools->disableDelete();
            // 去掉`查看`按钮
            $tools->disableView();

            $tools->disableList();
        });

        $form->footer(function ($footer) {

            // 去掉`重置`按钮
            $footer->disableReset();

            // 去掉`查看`checkbox
            $footer->disableViewCheck();

            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();

            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });


        return $form;
    }
}
